﻿$(document).ready(function () {
    //验证填写的信息
    //    $("#J_form input:visible").blur(function() {
    //        var t = $(this);
    //        var index = $.inArray(this, $.makeArray($("#J_form input:visible")));
    //        var reg = [/^[\S*]{1,20}$/, /^[\S*]{1,20}$/, /^[\S*]{1,20}$/, /^[\S*]{1,20}$/, /^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/, /^(((13[0-9]{1})|(15[0-35-9]{1})|(18[05-9]{1}))+\d{8})$/];
    //        var val = t.val().replace(/(^\s*)|(\s*$)/g, "");
    //        var result = (!reg[index].exec(val)) ? "form-showerror" : "form-showsuccess";
    //        t.nextAll(".form-message").removeClass("form-showerror").removeClass("form-showsuccess").addClass(result);
    //        t.nextAll("span.texts").html(t.val());
    //    });
    var J_light = $("#J_light");
    var J_form = $("#J_form");
    J_form.find("button[type='submit']").click(function () {
        this.blur();
    });
    //样式
    J_form.find("input[type='text']").focus(function () {
        $(this).addClass("form-text-onfocus");
    }).blur(function () {
        $(this).removeClass("form-text-onfocus");
    });
    //支付方式
    $("#J_payment_more").live("click", function () {
        $(this).parent().toggleClass("form-payment-open").find(".close").slideToggle(500);
    });
    //验证码
    var J_form_captcha = $("#J_form_captcha");
    J_form_captcha.find("img,span").click(function () {
        var url = J_form_captcha.attr("data-url-img") + "?random=" + Math.random();
        J_form_captcha.find("img").attr("src", url);
    }).click();
    J_form_captcha.find("input[type='text']").blur(function () {
        var t = $(this);
        var name = (!t.attr("name")) ? "captcha" : t.attr("name");
        var val = t.val();
        if (val.length < 1) {
            t.nextAll(".form-message").removeClass("form-showsuccess").addClass("form-showerror");
            return;
        };
        var url = J_form_captcha.attr("data-url-validator");
        var parm = { "random": Math.random };
        parm[name] = t.val();
        $.ajax({
            async: true,
            type: "POST",
            url: url,
            data: parm,
            dataType: "text",
            success: function (msg) {
                var data = msg;
                if (data == "true") {
                    t.nextAll(".form-message").removeClass("form-showerror").addClass("form-showsuccess");
                    return;
                };
                t.nextAll(".form-message").removeClass("form-showsuccess").addClass("form-showerror");
                J_form_captcha.find("img").click();
            },
            error: function () {
                alert("抱歉，出错了！请刷新页面后再试。");
            }
        });
    });
    //初始化表单状态
    $("#J_form_check").val("0");
    //提交信息
    J_form.submit(function () {
        var t = $(this);
        //如果检查通过，直接提交
        if ($("#J_form_check").val() == "1") { return true; }
        //验证填写信息
        //        var t = $(this);
        //        var data = {};
        //        var field1 = t.find("input:hidden");
        //        var count1 = field1.length;
        //        for (var i = 0; i < count1; i++) {
        //            field = field1.eq(i);
        //            data[field.attr("name")] = field.val();
        //        };
        //        var field2 = t.find("input:visible");
        //        var count2 = field2.length;
        //        for (var i = 0; i < count2; i++) {
        //            field = field2.eq(i);
        //            field.focus();
        //            field.blur();
        //            data[field.attr("name")] = field.val();
        //        };
        //        if (t.find(".form-showerror").length > 0) {
        //            return false;
        //        };
        //高亮检查
        var light = J_light;
        var light_box = J_light.find(".light-box");
        light.show();
        light.find(".light-bg").css("height", $(document).height());
        light.find(".light-iframe").css("height", $(document).height());
        var html = "<table>";
        var label = t.find("[data-tolight='label']");
        var content = t.find("[data-tolight='content']");
        var count = label.length;
        for (var i = 0; i < count; i++) {
            var a = "<tr><td class='label'>" + label.eq(i).html().replace(/<[^>]*>/ig, "").replace("*", "") + "</td><td>";
            var c = content.eq(i);
            if (c.find("input[type='radio']").length > 0) {
                a += c.find("input[type='radio']:checked").attr("title");
            } else if (c.find("input[type='text']").length > 0) {
                var ct = c.find("input[type='text']");
                var num = ct.length;
                for (var j = 0; j < num; j++) {
                    a += ct.eq(j).val();
                }
            } else if (c.find("select").length > 0) {
                var ct = c.find("select");
                var num = ct.length;
                for (var j = 0; j < num; j++) {
                    a += ct.eq(j).find("option:selected").text() + "/";
                };
                a = a.substring(0, a.length - 1);
            } else {
                a += c.html();
            }
            a += "</td></tr>";
            html += a;
        };
        html += "</table>";
        light.find(".light-form").html(html);
        light_box.css({ left: ($(window).width() - light_box.width()) / 2, top: ($(document).height() - light_box.height()) / 2 });
        return false;
    });

    J_light.find("button[type='submit']").click(function () {
        $("#J_form_check").val("1");
        J_form.submit();
    });
    J_light.find("button[type='reset']").click(function () {
        J_light.hide();
    });
    J_light.find(".light-close").click(function () {
        J_light.hide();
    });

    //购买协议
    var J_agreement = $("#J_agreement");
    var J_agreement_pop = $("#J_agreement_pop");
    J_agreement.find("a").click(function () {
        var t = $(this);
        var light = J_agreement_pop;
        var light_box = J_agreement_pop.find(".light-box");
        light.show();
        light.find(".light-bg").css("height", $(document).height());
        light.find(".light-iframe").css("height", $(document).height());
        light.find(".light-agreement").attr("src", t.attr("href"));
        light_box.css({ left: ($(window).width() - light_box.width()) / 2, top: ($(document).height() - light_box.height()) / 2 });
        return false;
    });
    J_agreement_pop.find(".light-close").click(function () {
        J_agreement_pop.hide();
    });
    //实名认证
    var J_shiming_pop = $("#J_shiming_pop");
    if (J_shiming_pop.length > 0) {
        var J_shiming_pop_box = J_shiming_pop.find(".light-box");
        J_shiming_pop.show();
        J_shiming_pop.find(".light-bg").css("height", $(document).height());
        J_shiming_pop.find(".light-iframe").css("height", $(document).height());
        J_shiming_pop_box.css({ left: ($(window).width() - J_shiming_pop_box.width()) / 2, top: ($(document).height() - J_shiming_pop_box.height()) / 2 });
    };
    J_shiming_pop.find(".light-close").click(function () {
        J_shiming_pop.hide();
    });
});